package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.demo.controller.from.MovieProductAddFrom;
import com.demo.exetion.MainExection;
import com.demo.model.MovieProduct;
import com.demo.dao.MovieProductDao;
import com.demo.service.MovieProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-04-05
 */
@Service
public class MovieProductServiceImpl extends ServiceImpl<MovieProductDao, MovieProduct> implements MovieProductService {


    @Autowired
    private RedisTemplate<String, Object> ort;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMovieProduct(MovieProductAddFrom movieProductAddFrom) {
        MovieProduct movieProduct = CommonUtil.copyOne(movieProductAddFrom, MovieProduct.class);
        movieProduct.setCreateTime(CommonUtil.dataLocalDatePares());
        int insert = baseMapper.insert(movieProduct);
        if (insert>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean onTheShelf(Integer id) throws MainExection {
        MovieProduct movieProduct = baseMapper.selectById(id);
        if (movieProduct==null) throw new MainExection(500,"电影不存在");
        if (movieProduct.getMovieShelf()==0) throw new MainExection(500,"电影已上架，请勿重复上架");
        movieProduct.setMovieShelf(0);
        int i = baseMapper.updateById(movieProduct);
        if (i <=0)throw new MainExection(500,"修改上架状态失败");
        ort.opsForValue().set("Movie:id:"+movieProduct.getProductId(),movieProduct.getMovieBoxOffice());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean offTheShelf(Integer id) throws MainExection {
        MovieProduct movieProduct = baseMapper.selectById(id);
        if (movieProduct==null) throw new MainExection(500,"电影不存在");
        if (movieProduct.getMovieShelf()==1) throw new MainExection(500,"电影已下架，请勿重复上架");
        movieProduct.setMovieShelf(1);
        Object o = ort.opsForValue().get("Movie:id:" + movieProduct.getProductId());
        if (o!=null){
            movieProduct.setMovieBoxOffice(o.toString());
        }
        int i = baseMapper.updateById(movieProduct);
        if (i <=0)throw new MainExection(500,"修改上架状态失败");
        Boolean delete = ort.delete("Movie:id:" + movieProduct.getProductId());
        if (!delete) throw new MainExection(500,"删除redis中键失败");
        return true;
    }

    @Override
    public MovieProduct selectOneMovieProduct(Integer id) throws MainExection {
        MovieProduct movieProduct = baseMapper.selectById(id);
        if (movieProduct==null) throw new MainExection(500,"该电影不存在");
        return movieProduct;
    }

    @Override
    public Boolean addMoney(Integer id, Double money) throws MainExection {
        MovieProduct movieProduct = baseMapper.selectById(id);
        if (movieProduct == null)throw new MainExection(500,"电影不存在");
        //获取redis中的金额
        Object o = ort.opsForValue().get("Movie:id:" + movieProduct.getProductId());
        if (o==null) {
            ort.opsForValue().set("Movie:id:"+movieProduct.getProductId(),movieProduct.getMovieBoxOffice());
            o=ort.opsForValue().get("Movie:id:" + movieProduct.getProductId());
        }
        double v = Double.parseDouble(o.toString());
        if (money <0){
            if (v<Math.abs(money)) throw new MainExection(500,"票房金额不能为负");
        }
        String newMoney=Double.toString(v+money);
        ort.opsForValue().set("Movie:id:"+movieProduct.getProductId(),newMoney);
        return true;
    }

}
